<template>
  <el-dialog v-if="load" :close-on-click-modal="false" title="货主比例" v-model="visible" align-center width="500px"
    :z-index="2000" @close="handleClose">
    <el-form label-width="60px" label-position="top" class="h-60">
      <el-form-item label="类型">
        <VSelect v-model="form.owner_percent_type" :options="options.owner_percent_type || []" class="w-full" />
      </el-form-item>
      <el-form-item v-if="form.owner_percent_type === 2" label="优先级">
        <div class="grid grid-cols-3 border-l border-t w-full">
          <template v-for="(d, i) in data" :key="i">
            <div class="p-1 border-b border-r text-center">{{ d.sale_name }}</div>
            <div class="p-1 border-b border-r col-span-2 flex gap-2">
              <el-input-number v-model="form.owner_percent[i + 1]" controls-position="right" :min="0" :step="1"
                :precision="0" class="w-full" />
            </div>
          </template>
        </div>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" :loading="loadings.submit" @click="submit">确 定</el-button>
    </template>
  </el-dialog>
</template>

<script setup>
import api from '../api'
import { ElMessage } from 'element-plus'
const props = defineProps({
  options: { type: Object, default: () => ({}) },
})

const load = ref(false)
const visible = ref(false)
let loadings = reactive({})
const form = ref({})
const data = ref([])

const emit = defineEmits(['close', 'success'])

// 打开弹窗
const open = async (row) => {
  console.log('row', row)
  if (!load.value) {
    load.value = true
    await 1
  }
  const { pd_id, pds_id, demand_info } = row
  data.value = demand_info
  loadings = reactive({})
  form.value = {
    pd_id, pds_id,
    owner_percent_type: 1,
    owner_percent: demand_info.reduce((acc, cur, idx) => (((acc[idx + 1] = ''), acc)), {})
  }
  visible.value = true
}
// 关闭弹窗
const close = () => {
  visible.value = false
}

const handleClose = () => {
  emit('close')
}
// 提交
const submit = () => {
  loadings.submit = true
  api.percent(form.value).then(res => {
    ElMessage.success('操作成功')
    close()
    emit('success')
  }).finally(() => {
    loadings.submit = false
  })
}

defineExpose({ open })
</script>